/*******************************************************************************
 *    系统名称   : 约汗乐动力接口/后台管理系统
 *    客户             : 约汗乐动力
 *    文件名        ： CookieUtil.java
 *              (C) Copyright 博智乐创 Corporation 2015
 *               All Rights Reserved.
 *              http://www.bozlc.com
 * *****************************************************************************
 *    注意： 本内容仅限于约汗乐动力软件公司内部使用，未经许可禁止转发
 ******************************************************************************/
package com.xiake.utils;

import java.math.BigDecimal;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// TODO: Auto-generated Javadoc

/**
 * The Class CookieUtil.
 *
 * @author 齐龙
 * @createdate 2015-4-25
 * 
 * cookie 操作工具类
 */
public class CookieUtil {
    
    /** The user mis. */
    public static String USER_MIS = "userMis";
    
    /** The Constant MAX_AGE. */
    final static int MAX_AGE = 7 * 24 * 60 * 60;//默认cookie生存期 7天
    
    /**
     *  
     * 添加新的Cookie .
     *
     * @param name the name
     * @param value the value
     * @param response the response
     */  
    public static void setNewCookie(String name,String value,HttpServletResponse response) {  
        Cookie cookie=new Cookie(name,value);  
        cookie.setPath("/");
        cookie.setMaxAge(MAX_AGE);//设置生存期，当设置为负值时，则为浏览器进程Cookie(内存中保存)，关闭浏览器就失效。      
        response.addCookie(cookie);  
    } 
    
    /**
     *  
     * 添加新的Cookie .
     *
     * @param name the name
     * @param value the value
     * @param maxAge the max age
     * @param domain the domain
     * @param response the response
     */  
    public static void setNewCookie(String name,String value,int maxAge,String domain, HttpServletResponse response) {  
        Cookie cookie=new Cookie(name,value); 
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);//设置生存期，当设置为负值时，则为浏览器进程Cookie(内存中保存)，关闭浏览器就失效。  
        if(domain != null && !domain.isEmpty()){
        	cookie.setDomain(domain);
        }
        response.addCookie(cookie);  
    } 
    
    /**
     *  
     * 获取Cookie值 .
     *
     * @param name the name
     * @param request the request
     * @return the cookie value
     */  
    public static String getCookieValue(String name,HttpServletRequest request) {  
        String value="";  
        Cookie[] cookies=request.getCookies(); 
        if(null!=cookies){
            for (int i = 0; i < cookies.length; i++) {  
                if (cookies[i].getName().equalsIgnoreCase(name)) {  
                    value=cookies[i].getValue();  
                }  
            }
        }
        return value;  
    } 
    
    /**
     *  
     * 删除cookie中对应数值 .
     *
     * @param name the name
     * @param request the request
     * @param response the response
     */  
    public static void  deleteCookie(String name,HttpServletRequest request,HttpServletResponse response) {  
        Cookie[] cookies=request.getCookies();  
        for (int i = 0; i < cookies.length; i++) {  
            if (cookies[i].getName().equalsIgnoreCase(name)) {  
                cookies[i].setValue(null);  
                cookies[i].setPath("/");
                cookies[i].setMaxAge(0);//设置为0为立即删除该Cookie   
                response.addCookie(cookies[i]);  
            }  
        }  
    }  
      
      
    /**
     *  
     * 增加规定cookie中的数值 .
     *
     * @param name the name
     * @param value the value
     * @param request the request
     * @param response the response
     */  
    public static void addnewCookieValue(String name,BigDecimal value,HttpServletRequest request,HttpServletResponse response) {  
        Cookie[] cookies=request.getCookies();  
        for (int i = 0; i < cookies.length; i++) {                   
            if (cookies[i].getName().equalsIgnoreCase(name)) {  
                cookies[i].setValue(new BigDecimal(cookies[i].getValue().trim()).add(value).toString());  
                response.addCookie(cookies[i]);  
            }  
        }  
    }  
      
    /**
     *  
     * 减去规定cookie中的数值 .
     *
     * @param name the name
     * @param value the value
     * @param request the request
     * @param response the response
     */  
    public static void subCookieValue(String name,BigDecimal value, HttpServletRequest request,HttpServletResponse response) {  
        Cookie[] cookies=request.getCookies();  
        for (int i = 0; i < cookies.length; i++) {  
            if (cookies[i].getName().equalsIgnoreCase(name)) {  
                cookies[i].setValue(new BigDecimal(cookies[i].getValue()).subtract(value).toString());  
                response.addCookie(cookies[i]);  
            }  
        }  
    }
}
